package com.hustbaidu.hustmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnLongClickListener;

public class InfoWindowSurfaceView extends SurfaceView implements OnCreateContextMenuListener, OnMenuItemClickListener, SurfaceHolder.Callback {
	private SurfaceHolder holder;
	private String text;
	private Bitmap mbitmap;
	private boolean downFlag = false;
	private GestureDetector myGestureDetector;
	
	public int offsetX, offsetY;
	public int width, height;
	
	public InfoWindowSurfaceView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public InfoWindowSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public InfoWindowSurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public InfoWindowSurfaceView(Context context, String text) {
		super(context);
		// TODO Auto-generated constructor stub
		System.out.println("InfoWindowSurfaceView constructor");
		this.text = text;
		this.offsetX = 200;
		this.offsetY = 400;
		this.width = 400;
		this.height = 200;
		initBitmap();
		setZOrderOnTop(true);
//		mbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
		myGestureDetector = new GestureDetector(new MyGestureListener());
		holder = this.getHolder();
		holder.setFormat(PixelFormat.TRANSPARENT);
		holder.addCallback(this);	
		setOnCreateContextMenuListener(this);
		
	}

	@Override
	public void setOnLongClickListener(OnLongClickListener l) {
		// TODO Auto-generated method stub
		System.out.println("setOnLongClickListener");
		super.setOnLongClickListener(l);
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		myGestureDetector.onTouchEvent(event);
		return true;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		System.out.println("InfoWindowSurfaceView surfaceCreated");
		updateView();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}
	
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		menu.setHeaderTitle("操作");
		menu.add(0, 0, 0, "详细介绍").setOnMenuItemClickListener(this);
		menu.add(0, 1, 0, "自定义信息").setOnMenuItemClickListener(this);
	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		// TODO Auto-generated method stub
		return false;
	}

	private void updateView(){
		Canvas c = holder.lockCanvas();
		c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//		c.drawBitmap(mbitmap, 0, 0, null);
		Rect src = new Rect(0, 0, mbitmap.getWidth(), mbitmap.getHeight());
		Rect dst = new Rect(0, 0, mbitmap.getWidth(), mbitmap.getHeight());
		c.drawBitmap(mbitmap, src, dst, null);
//		System.out.println(mbitmap.getWidth() + " : " + mbitmap.getHeight());
//		System.out.println(c.getWidth() + " : " + c.getHeight());
		
		holder.unlockCanvasAndPost(c);
		System.out.println("updateView over");
	}
	
	private void initBitmap(){
		mbitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas cv = new Canvas(mbitmap);
		
		Path p = new Path();
		
		RectF r = new RectF(0,0,20,20);
		p.addArc(r, 180, 90);
		r.offsetTo(280, 0);
		p.addArc(r, 270, 90);
		r.offsetTo(280, 130);
		p.addArc(r, 0, 90);
		r.offsetTo(0, 130);
		p.addArc(r, 90, 90);
		p.moveTo(10, 0);
		p.lineTo(290, 0);
		p.moveTo(300, 10);
		p.lineTo(300, 140);
		p.moveTo(290, 150);
		p.lineTo(160, 150);
		p.lineTo(50, 200);
		p.lineTo(80, 150);
		p.lineTo(10, 150);
		p.moveTo(0, 140);
		p.lineTo(0, 10);

		Paint paint = new Paint();
		
		//���������
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(2);
		paint.setColor(Color.rgb(0, 0, 0));
		
		cv.drawPath(p, paint);
		paint.setStrokeWidth(3);
		cv.drawLine(10, 0, 290, 0, paint);
		cv.drawLine(0, 10, 0, 140, paint);
		cv.drawLine(300, 10, 300, 140, paint);
		
		paint.setStrokeWidth(1);
		
		p.reset();
		r = new RectF(1,1,19,19);
		p.addArc(r, 180, 90);
		r.offsetTo(279, 1);
		p.addArc(r, 270, 90);
		r.offsetTo(279, 129);
		p.addArc(r, 0, 90);
		r.offsetTo(1, 129);
		p.addArc(r, 90, 90);
		p.moveTo(10, 2);  //A
		p.lineTo(290, 2);  //B
		p.lineTo(298, 10); //C
		p.lineTo(298, 140.5f); //D
		p.lineTo(290, 149.5f);  //E
		p.lineTo(159, 149.5f); //F
		p.lineTo(51, 199.5f);  //G
		p.lineTo(81, 149.5f);  //H
		p.lineTo(10, 149.5f);  //I
		p.lineTo(2, 140.5f);  //J
		p.lineTo(2, 10);  //K
		p.lineTo(10, 1);  //A
		
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.argb(255, 191, 198, 208));
		cv.drawPath(p, paint);
		
		paint.setColor(Color.rgb(49, 49, 49));
		paint.setStyle(Paint.Style.FILL);
		paint.setTextSize(18);
		
		
		if(text == null) text = "";
		
		/*
		 * ������ʵ���ı��Զ����еĴ���
		 */
		FontMetrics fm = paint.getFontMetrics();
		float baseline = fm.descent - fm.ascent;
		float x = 13f;
		float y = 24f;
		
		/*
		 * ��ΪҪ���ϱ߿�ı߾࣬��������Ŀ�ȼ�20
		 */
		String[] texts = autoSplit(text, paint, width - 40);
		for(String text : texts)
		{
			cv.drawText(text, x, y, paint);
			y += baseline + fm.leading;
//			System.out.println("draw Text fm.leading is : " + fm.leading + "\n" + "baseline is : " +baseline);
		}
	}
	
	/*
	 * �������ı���ֵķ���
	 */
	private String[] autoSplit(String string, Paint paint, float width)
	{
		int length = string.length();
		float textWidth = paint.measureText(string);
		if ( textWidth <= width)
		{
			System.out.println(textWidth + " : " + width);
			return new String[]{string};
		}
		
		int start = 0, end = 1, i = 0;
		int lines = (int) Math.ceil(textWidth / width);
		String[] lineTexts = new String[lines];
		while( start < length)
		{
			if ( paint.measureText(string , start , end) > width)
			{
				lineTexts[i++] = (String) string.substring(start, end);
				start = end;
			}
			if (end == length )
			{
				lineTexts[i] = (String) string.subSequence(start, end);
				break;
			}
			end += 1;
		}
		return lineTexts;
	}

	public void move(int distanceX, int distanceY)
	{
		offsetX = offsetX - distanceX;
		offsetY = offsetY - distanceY;
		this.layout(offsetX, offsetY, offsetX + width, offsetY + height);
	}
	
	private class MyGestureListener extends SimpleOnGestureListener{
		
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// TODO Auto-generated method stub
			
			System.out.println("onSingleTapConfirmed");
			return super.onSingleTapConfirmed(e);
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			System.out.println("onSingleTapUp");
			return super.onSingleTapUp(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			return super.onScroll(e1, e2, distanceX, distanceY);
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			showContextMenu();
			super.onLongPress(e);
		}
		
	}

//	@Override
//	protected void onCreateContextMenu(ContextMenu menu) {
//		// TODO Auto-generated method stub
//		super.onCreateContextMenu(menu);
//		menu.add("��ϸ");
//	}

//	@Override
//	public void setOnClickListener(OnClickListener l) {
//		// TODO Auto-generated method stub
//		super.setOnClickListener(l);
//		
//	}
	
	
}
